<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class CmdTaskKillWindow extends CI_Controller {
	
	function __construct() {
		parent::__construct();
		// used in the view
		$this->load->helper('url');		
	}
	
	public function index()	{
		
		set_time_limit(0);
		// print_r("test")	;
		$logName = '';
				
		foreach($_GET as $key => $value) {
			
	        if ($key != null) {
	        		
	        	switch ($key) {
					case 'ln':
						// print_r('</br>key: ordId </br>');
						$logName = $value;
						print_r('Oczekiwanie na plik z logiem: <b>'.$logName.'</b></br>');
						// print_r('</br>logName: '.$logName.' </br>');
			        	break;
		        	default: 
	        	}
	        	
	        } else {
	        	 // echo '<br/>elseValue: '.$key.'<br/>';
	        }
	    }
		
		// print_r('</br>');
		$folderName = substr($logName, 4, 14);
		// print_r('</br>');
		// print_r($folderName);
		// print_r('</br>');		
		$pathToFile = 'logs/'.$folderName.'/'.$logName;
		
		// print_r($pathToFile);
		while (!file_exists($pathToFile)) {
			sleep(1);
		}
		
		sleep(5);
		
		$shellExecCommand = 'START /WAIT TASKKILL /F /FI "WINDOWTITLE eq '.$logName.'"';
		//$shellExecCommand = 'taskkill  /f /fi "'.$logName.'"';
		//print_r('shellExecCommand: '.$shellExecCommand);
		shell_exec($shellExecCommand);
		// print_r('<br/>WINDOWTITLE TO KILL: '.$logName);
		echo "<script>window.close();</script>";
		
		$this->session->sess_destroy();
	}
}